<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'recent_posts_widget' );

/* Register widget */
function recent_posts_widget()
{
	register_widget( 'recent_posts_widget' );
}

/* Handler class for all widget params */
class recent_posts_widget extends WP_Widget
{

	/* Widget setup. */
	function recent_posts_widget()
	{
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'recent_posts_widget', 'description' => __('An image gallery widget.', 'recent_posts_widget') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 400, 'id_base' => 'recent_posts_widget' );
		/* Create the widget. */
		$this->WP_Widget( 'recent_posts_widget', 'Laz - Recent Posts', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance ) {
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );
		if ( isset($instance['id']) ) $id = $instance['id'];
		$max_posts = $instance['posts_count'];

		/* Widget themplate */
		echo $before_widget;
		
	   	if ( $title ) echo $before_title . $title . $after_title;

		echo '<div class="recent_posts">';
		
			
			$current_categories = '';
			foreach ( $instance['posts_categories'] as $cat )
			{
				$current_categories .= $cat.',';
			}
			
			$wp_query = new WP_Query('post_type=post&posts_per_page='.$max_posts.'&cat='.$current_categories);

			if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
					
				if ( has_post_thumbnail() ) $thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id( ) );
				
				$image_source = get_template_directory_uri().'/includes/timthumb.php?src='.$thumbnail_src.'&amp;w=70&amp;h=70&amp;zc=1&amp;q=100';
				echo '<div class="post_entry clearfix">';
					echo '<div class="post_image">';
						echo '<a href="'; the_permalink(); echo '">';
							echo '<img src="'.$image_source.'" alt="'; echo the_title(); echo '" />';
						echo '</a>';
					echo '</div>';
					echo '<div class="post_content">';
						echo '<div class="post_title">';
							echo '<a href="'; the_permalink(); echo '">';
								echo the_title();
							echo '</a>';
						echo '</div>';
						echo '<div class="post_date">';
							echo get_the_date('jS F Y'); 
						echo '</div>';
					echo '</div>';
				echo '</div>';

			endwhile; endif;

		echo '</div>';
		
		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance )
	{
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['posts_count'] = $new_instance['posts_count'];
		$instance['posts_categories'] = $new_instance['posts_categories'];

		return $instance;
	}

	/* Admin panel form  */
	function form( $instance )
	{
		$instance = wp_parse_args( (array) $instance );

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('posts_count'); echo '">'; echo 'Number of posts'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('posts_count'); echo '" name="'; echo $this->get_field_name('posts_count'); echo '" value="'; if( isset($instance['posts_count']) ) echo $instance['posts_count']; echo '" />';
		echo '</p>';
		
		$categories =  get_categories();
		foreach ($categories as $cat)
		{
			$option='<input type="checkbox" id="'. $this->get_field_id( 'posts_categories' ) .'[]" name="'. $this->get_field_name( 'posts_categories' ) .'[]"';
			
			if (is_array($instance['posts_categories']))
			{
				foreach ($instance['posts_categories'] as $cats)
				{
					if ($cats == $cat->term_id)
					{
						$option = $option.' checked="checked"';
					}
				}
			}
			$option .= ' value="'.$cat->term_id.'" />';

			$option .= $cat->cat_name;
			$option .= ' ('.$cat->category_count.')';
			$option .= '<br />';
			
			echo $option;
		}
		
	}
}

?>